package com.zhonghui.scm.service.impl;

import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.scm.domain.ScmPurchaseArrival;
import com.zhonghui.scm.domain.ScmPurchaseArrivalItem;
import com.zhonghui.scm.domain.ScmPurchaseReturn;
import com.zhonghui.scm.domain.ScmPurchaseReturnItem;
import com.zhonghui.scm.mapper.ScmPurchaseArrivalMapper;
import com.zhonghui.scm.mapper.ScmPurchaseReturnMapper;
import com.zhonghui.scm.service.IScmPurchaseReturnService;
import com.zhonghui.wms.domain.WmsMaterial;
import com.zhonghui.wms.mapper.WmsMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 采购退货Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmPurchaseReturnServiceImpl implements IScmPurchaseReturnService
{
    @Autowired
    private ScmPurchaseReturnMapper scmPurchaseReturnMapper;

    @Autowired
    private ScmPurchaseArrivalMapper scmPurchaseArrivalMapper;

    @Autowired
    private WmsMaterialMapper wmsMaterialMapper;

    /**
     * 查询采购退货
     *
     * @param id 采购退货主键
     * @return 采购退货
     */
    @Override
    public ScmPurchaseReturn getById(Long id)
    {
        return scmPurchaseReturnMapper.getById(id);
    }

    /**
     * 查询采购退货列表
     *
     * @param scmPurchaseReturn 采购退货
     * @return 采购退货
     */
    @Override
    public List<ScmPurchaseReturn> getList(ScmPurchaseReturn scmPurchaseReturn)
    {
        return scmPurchaseReturnMapper.getList(scmPurchaseReturn);
    }

    /**
     * 新增采购退货
     *
     * @param scmPurchaseReturn 采购退货
     * @return 结果
     */
    @Override
    public int add(ScmPurchaseReturn scmPurchaseReturn)
    {
        scmPurchaseReturn.setReturnNo(generateNumber("CGTH"));

        ScmPurchaseArrival scmPurchaseArrival = scmPurchaseArrivalMapper.getById(scmPurchaseReturn.getPurchasingArrivalId());
        for (ScmPurchaseArrivalItem scmPurchaseArrivalItem : scmPurchaseArrival.getScmPurchaseArrivalItemList()) {
            for (ScmPurchaseReturnItem scmPurchaseReturnItem : scmPurchaseReturn.getScmPurchaseReturnItemList()) {
                if (scmPurchaseArrivalItem.getMaterialId().equals(scmPurchaseReturnItem.getMaterialId())) {
                    scmPurchaseArrivalItem.setReturnQuantity(scmPurchaseReturnItem.getReturnQuantity());
                    scmPurchaseArrivalMapper.editItemOne(scmPurchaseArrivalItem);
                }
            }
        }
        List<ScmPurchaseReturnItem> scmPurchaseReturnItemList = scmPurchaseReturn.getScmPurchaseReturnItemList();
        BigDecimal amount = new BigDecimal("0");
        if (scmPurchaseReturnItemList != null && scmPurchaseReturnItemList.size() > 0) {
            for (ScmPurchaseReturnItem item : scmPurchaseReturnItemList) {
                WmsMaterial wmsMaterial = wmsMaterialMapper.selectWmsMaterialById(item.getMaterialId());
                amount = amount.add((wmsMaterial.getMaterialPrice().multiply(new BigDecimal(item.getReturnQuantity()))));
            }
        }
        scmPurchaseReturn.setReturnAmount(amount);
        scmPurchaseReturn.setFounderId(SecurityUtils.getUserId());

        int row = scmPurchaseReturnMapper.add(scmPurchaseReturn);
        insertItem(scmPurchaseReturn);
        return row;
    }

    /**
     * 修改采购退货
     *
     * @param scmPurchaseReturn 采购退货
     * @return 结果
     */
    @Override
    public int edit(ScmPurchaseReturn scmPurchaseReturn)
    {
        scmPurchaseReturnMapper.delItem(scmPurchaseReturn.getId());
        insertItem(scmPurchaseReturn);
        List<ScmPurchaseReturnItem> scmPurchaseReturnItemList = scmPurchaseReturn.getScmPurchaseReturnItemList();
        BigDecimal amount = new BigDecimal("0");
        if (scmPurchaseReturnItemList != null && scmPurchaseReturnItemList.size() > 0) {
            for (ScmPurchaseReturnItem item : scmPurchaseReturnItemList) {
                WmsMaterial wmsMaterial = wmsMaterialMapper.selectWmsMaterialById(item.getMaterialId());
                amount = amount.add((wmsMaterial.getMaterialPrice().multiply(new BigDecimal(item.getReturnQuantity()))));
            }
        }
        scmPurchaseReturn.setReturnAmount(amount);
        return scmPurchaseReturnMapper.edit(scmPurchaseReturn);
    }

    /**
     * 批量删除采购退货
     *
     * @param ids 需要删除的采购退货主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmPurchaseReturnMapper.delItem(id);
        }
        return scmPurchaseReturnMapper.del(ids);
    }


    public void insertItem(ScmPurchaseReturn scmPurchaseReturn){
        List<ScmPurchaseReturnItem> scmPurchaseReturnItemList = scmPurchaseReturn.getScmPurchaseReturnItemList();

        if (!scmPurchaseReturnItemList.isEmpty()){

        for (ScmPurchaseReturnItem scmPurchaseReturnItem : scmPurchaseReturnItemList) {
            scmPurchaseReturnItem.setPurchasingReturnId(scmPurchaseReturn.getId());
        }
        scmPurchaseReturnMapper.addItem(scmPurchaseReturnItemList);

        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }

}
